<template>
  <!-- 评教管理 -->
  <div>
    <div class="edu-page-warp">
      <condition-card @reset="reset" @search="searchAll" :label-width="labelWidth" style="margin-bottom: 20px;">
        <el-row :gutter="24">
          <!-- 学院 -->
          <el-col :span="8">
            <el-form-item label="学院">
              <DictSelect
                  labelAll="请选择"
                  :disabled="facultyDisable"
                  filterable multiple collapse-tags
                  v-model="queryParams.facultyList"
                  :data="options.faculty"
                  @change="handleChangeFaculty"
              ></DictSelect>
            </el-form-item>
          </el-col>
          <!-- 职称 -->
          <el-col :span="8">
            <el-form-item label="职称">
              <el-select v-model="queryParams.titleList" filterable collapse-tags multiple style="width: 100%">
                <el-option
                  v-for="item in options.title"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 性别 -->
          <el-col :span="8">
            <el-form-item label="性别">
              <el-select v-model="queryParams.sex" style="width: 100%">
                <el-option label="男" value="1"></el-option>
                <el-option label="女" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <!-- 职工号/姓名 -->
          <el-col :span="8">
            <el-form-item label="职工号/姓名">
              <el-input v-model="queryParams.teacherCodeOrName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <!-- 是否有研究生授课资格 -->
          <el-col :span="8">
            <el-form-item label="是否有研究生授课资格">
              <el-select v-model="queryParams.postTeach" style="width: 100%">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 导师类型 -->
          <el-col :span="8">
            <el-form-item label="导师类型">
              <el-select v-model="queryParams.teacherType" filterable collapse-tags multiple style="width: 100%">
                <el-option label="硕导" value="1"></el-option>
                <el-option label="博导" value="2"></el-option>
                <el-option label="硕博导" value="3"></el-option>
                <el-option label="其他" value="其他"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <!-- 是否有在职研究生授课资格 -->
          <el-col :span="8">
            <el-form-item label="是否有在职研究生授课资格">
              <el-select v-model="queryParams.postTeachOnJob" style="width: 100%">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>

      <!-- 表格card -->
      <el-card shadow="never">
        <div class="edu-opertion-bar">
          <div class="float-left edu-page-title">{{titleDone}}</div>
          <div class="float-right">
            <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
              <el-button type="danger" @click="deleteObjectClick">{{$t('移除')}}</el-button>
            </el-row>
          </div>
        </div>
        <!-- 主页表格 -->
        <edu-table :url="doneUrl" ref="doneTable" :order="true" :selection="true">
          <edu-table-column :label="$t('学院')" prop="facultyName"></edu-table-column>
          <edu-table-column :label="$t('职称')" prop="titleName"></edu-table-column>
          <edu-table-column :label="$t('性别')" prop="sexName"></edu-table-column>
          <edu-table-column :label="$t('是否有研究生授课资格')" min-width="160px" prop="postTeachName"></edu-table-column>
          <edu-table-column :label="$t('是否有在职研究生授课资格')" min-width="180px" prop="postTeachOnJobName"></edu-table-column>
          <edu-table-column :label="$t('导师类型')" prop="teacherTypeName"></edu-table-column>
          <edu-table-column :label="$t('工号')" prop="teacherCode"></edu-table-column>
          <edu-table-column :label="$t('姓名')" prop="teacherName"></edu-table-column>
        </edu-table>
      </el-card>

      <!-- 表格card -->
      <el-card shadow="never">
        <div class="edu-opertion-bar">
          <div class="float-left edu-page-title">{{titleNot}}</div>
          <div class="float-right">
            <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
              <el-button type="primary" @click="save">{{$t('添加')}}</el-button>
            </el-row>
          </div>
        </div>
        <!-- 主页表格 -->
        <edu-table :url="url" ref="mainTable" :order="true" :selection="true">
          <edu-table-column :label="$t('学院')" prop="facultyName"></edu-table-column>
          <edu-table-column :label="$t('职称')" prop="titleName"></edu-table-column>
          <edu-table-column :label="$t('性别')" prop="sexName"></edu-table-column>
          <edu-table-column :label="$t('是否有研究生授课资格')" min-width="160px" prop="postTeachName"></edu-table-column>
          <edu-table-column :label="$t('是否有在职研究生授课资格')" min-width="180px" prop="postTeachOnJobName"></edu-table-column>
          <edu-table-column :label="$t('导师类型')" prop="teacherTypeName"></edu-table-column>
          <edu-table-column :label="$t('工号')" prop="teacherCode"></edu-table-column>
          <edu-table-column :label="$t('姓名')" prop="teacherName"></edu-table-column>
        </edu-table>
        <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
          <el-button type="primary" @click="prevStep">{{$t('上一步')}}</el-button>
          <el-button @click="cancel">{{$t('返回')}}</el-button>
        </el-row>
      </el-card>
    </div>
  </div>
</template>
<script>
import UTILS from "common/src/utils/utils";
import baseUrl from 'common/src/api/teachingEvaluation/env'
import { queryDic } from "common/src/api/dictionary"
import processEvaluationApi from 'common/src/api/teachingEvaluation/processEvaluation'
export default {
  props: {
    ifEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const getGrade = (function() {
      let now = new Date().getFullYear();
      let years = [];
      for (let i = 0; i < 6; i++) {
        years.push({
          label: now - i,
          value: now - i
        });
      }
      return years;
    })();
    return {
      routeParams: {},
      titleNot: '未配置对象列表',
      titleDone: '已配置对象列表',
      url: `${baseUrl}/processTeacher/object/getPageList`,
      doneUrl: `${baseUrl}/processTeacher/object/look/getPageList`,
      labelWidth: '210px',
      currentCalendarId: '',
      queryParams: {
        facultyList: [],
        titleList: [],
        sex: '',
        teacherCodeOrName: '',
        postTeach: '',
        teacherType: [],
        postTeachOnJob: '',
      },
      options: {
        title: [],
        faculty: []
      },
      facultyList: [],
      dataList: [],
      facultyDisable: false
    }
  },
  created() {
    if (sessionStorage.getItem('currFacultyCode')) {
      this.queryParams.facultyList.push(sessionStorage.getItem('currFacultyCode'))
      this.facultyDisable = true
    }
    this.getOptions();
  },
  mounted() {
    if (this.$route.query.managementId) {
      this.queryParams.managementId = this.$route.query.managementId;
      this.queryParams.calendarId = this.$route.query.calendarId;
      this.searchAll()
    }
  },
  methods: {
    // 查询两个表格数据
    searchAll() {
      this.searchList()
      this.searchDoneList()
    },
    // 查询
    searchList() {
      this.$refs.mainTable.pageNum = 1;
      this.$refs.mainTable.search(this.queryParams)
    },
    // 查询
    searchDoneList() {
      this.$refs.doneTable.pageNum = 1;
      this.$refs.doneTable.search(this.queryParams)
    },
    // 重置查询参数
    reset() {
      this.queryParams = {
        managementId: this.$route.query.managementId ? this.$route.query.managementId : this.routeParams.managementId,
        calendarId: this.$route.query.calendarId ? this.$route.query.calendarId : this.routeParams.calendarId,
        facultyList: [],
        titleList: [],
        sex: '',
        teacherCodeOrName: '',
        postTeach: '',
        teacherType: [],
        postTeachOnJob: '',
      };
      if (this.facultyDisable) {
        this.queryParams.facultyList.push(sessionStorage.getItem('currFacultyCode'))
      }
      this.searchAll()
    },
    // 获取字典数据
    getOptions() {
      //筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: [
          "profession", // 职称
          "X_YX"
        ]
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            title: "profession", // 职称
            faculty: "X_YX"
          },
          res.data,
          this.options
        );
      });
    },
    // 取消 直接返回列表页
    cancel() {
      this.$emit('onlyGoBack')
      this.$router.push({
        query: {}
      })
    },
    // 上一步 返回上一页
    prevStep() {
      if (this.ifEdit) {
        this.$emit('editGoBack')
      } else {
        this.$emit('goToAddOrEdit')
      }
    },
    // 对象配置最后一次保存
    save() {
      const checkDatas = this.$refs.mainTable.getCheckData();
      let param = Object.assign({},this.queryParams)
      param.objectList = JSON.parse(JSON.stringify(checkDatas))
      processEvaluationApi.objectTeacherProcessEvaSetConfirm(param).then(res => {
        if (res.code == 200) {
          this.$message.success(this.$t('新增过程评价对象配置成功，请稍后发布'))
          this.searchAll()
        }
      })
    },
    // 删除对象配置
    deleteObjectClick() {
      this.$confirm(this.$t('确定要移除已配置的这些对象吗？'), this.$t("message.prompt"), {
        confirmButtonText: this.$t("message.confirm"),
        cancelButtonText: this.$t("message.cancel"),
        type: "warning"
      }).then(() => {
        const checkDatas = this.$refs.doneTable.getCheckData();
        let param = Object.assign({},this.queryParams)
        param.objectList = JSON.parse(JSON.stringify(checkDatas))
        processEvaluationApi.objectTeacherProcessEvaSetDelete(param).then(res => {
          if (res.code == 200) {
            this.$message.success(this.$t('对象删除成功，请稍后发布'))
            this.searchAll()
          }
        })
      })
    }
  },
  watch: {
    '$route'(to, from) {
        if (Object.values(to.query).length > 0) {
          this.routeParams = _.cloneDeep(to.query)
          if (this.routeParams.managementId) {
            this.queryParams.managementId = this.routeParams.managementId;
            this.queryParams.calendarId = this.routeParams.calendarId;
            this.searchAll()
          }
        }
    }
  }
}
</script>
<style lang="scss">
</style>
